﻿using System;
using System.Globalization;

namespace BoxSauce.AgencyToolkit.Date
{
    /// <summary>
    /// 
    /// </summary>
    public static class DateHelpers
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="format"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static string StringFromPredefinedFormat(DateTime dateTime, DateTimeStringFormat format, CultureInfo culture)
        {
            if (dateTime == null)
                throw new ArgumentNullException("dateTime", "DateTime cannot be null");

            switch (format)
            {
                case DateTimeStringFormat.ShortDate:
                    return dateTime.ToString("dd/MM/yy", culture);

                case DateTimeStringFormat.LongDate:
                    return dateTime.ToString("dd MMMM yyyy", culture);

                case DateTimeStringFormat.Time12H:
                    return dateTime.ToString("h:mm", culture);

                case DateTimeStringFormat.Time24H:
                    return dateTime.ToString("HH:m", culture);
            }

            return dateTime.ToString(CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="cultute"></param>
        /// <returns></returns>
        public static string StringInFriendlyFormat(DateTime dateTime, CultureInfo cultute)
        {
            return StringInFriendlyFormat(dateTime, cultute, new FriendlyDateFormatValues());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dateTime"></param>
        /// <param name="values"></param>
        /// <param name="cultute"></param>
        /// <returns></returns>
        public static string StringInFriendlyFormat(DateTime dateTime, CultureInfo cultute, FriendlyDateFormatValues values)
        {
            if (dateTime == null)
                throw new ArgumentNullException("dateTime", "DateTime cannot be null");

            var timespan = DateTime.Now.Subtract(dateTime);

            if (timespan.TotalMinutes < 2)
                return values.MinuteAgo;

            if (timespan.TotalMinutes < 60)
                return string.Format(values.MinutesAgo, timespan.TotalMinutes.ToString("0"));

            if (timespan.TotalHours < 2)
                return values.HourAgo;

            if (timespan.TotalHours < 24)
                return string.Format(values.HoursAgo, timespan.TotalHours.ToString("0"));

            if (timespan.TotalDays < 2)
                return values.DayAgo;

            return string.Format(values.DaysAgo, timespan.TotalDays.ToString("0"));
        }
    }
}
